package com.lintcode.solution;

public class Solution_141 {
    public static void main(String[] args) {
        double x = 63256546665456.0;
        System.out.println(sqrt(x));
        System.out.println(Math.sqrt(x));
        //        System.out.println(Math.pow(3, 0.5));
    }
    
    public static double sqrt(double x, double p) {
        double b = 1.0;
        double cheak;
        do {
            b = (x / b + b) / 2.0;
            cheak = b * b - x;
        } while ((cheak >= 0 ? cheak : - cheak) > p);
        return b;
    }
    
    public static double sqrt(double x) {
        double b = 1.0;
        for (int i = 1; i <= String.valueOf(x).length() * String.valueOf(x).length(); i++) {
            b = 0.5 * (x / b + b);
        }
        return b;
    }
}
